package com.runfast.waimai.web.controller.shopper;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.paotui.dao.model.Order;
import com.runfast.paotui.service.OrderService;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.service.WaiMaiRunfastShopperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * ${DESCRIPTION} 骑手
 *
 * @author chenzheng
 * @create 2018-05-17 17:59
 **/
@RestController("shopperController")
@RequestMapping("/server/shopper")
public class ShopperController {
    @Autowired
    private WaiMaiRunfastShopperService waiMaiRunfastShopperService;
    @Autowired
    private RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService runfastAgentbusinessService;
    @Autowired
    private OrderService orderService;
    /**
     * 骑手列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, WaiMaiRunfastShopper shopper){
        WaiMaiRunfastShopperExample example=new WaiMaiRunfastShopperExample();
        if(shopper !=null){
            if(shopper.getWorking()!=null){
                if(shopper.getWorking()==1){
                    example.createCriteria().andWorkingEqualTo(1);//1上班，其他休息
                }else{
                    example.createCriteria().andWorkingNotEqualTo(1);
                }
            }
            if(shopper.getName()!=null){
                example.createCriteria().andNameLike(shopper.getName());
            }
            if(shopper.getWorkType()!=null){
                if(shopper.getWorkType()==0){
                    example.createCriteria().andWorkTypeEqualTo(0);//全职
                }else if(shopper.getWorkType()==1){
                    example.createCriteria().andWorkTypeEqualTo(1);//兼职
                }
            }
            if(shopper.getStatus()!=null){
                if(shopper.getStatus() !=-1){
                    //1/null：正常状态，-1：冻结状态(对应新需求的停用---冻结状态，启用--正常状态)
                    example.createCriteria().andStatusNotEqualTo(-1);
                }else if(shopper.getStatus()==-1){
                    example.createCriteria().andStatusEqualTo(-1);
                }
            }

        }
        List<WaiMaiRunfastShopper> list=waiMaiRunfastShopperService.selectByExampleWithPageable(example,pageable);
        Integer total=waiMaiRunfastShopperService.countByExample(example);

        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
     }

    /**
     * 骑手列表
     */
    @GetMapping("/select")
    //@RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        //List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        //return Result.ok(null,list);
        return null;
    }

    /**
     * 保存骑手
     */
    @PostMapping("/save")
    //@RequiresPermissions("server:goodsSellType:save")
    public Result save(WaiMaiRunfastShopper shopper){
        shopper.setCreatetime(new Date());
        shopper.setDistributionmun(5);
        waiMaiRunfastShopperService.insertSelective(shopper);
        return Result.ok(null);
    }

    /**
     * 修改骑手
     */
    @PostMapping("/update")
    //@RequiresPermissions("server:goodsSellType:update")
    public Result update(WaiMaiRunfastShopper shopper){
        waiMaiRunfastShopperService.updateByPrimaryKeySelective(shopper);
        return Result.ok(null);
    }

    /**
     * 删除骑手---暂时不实现
     */
    /*@PostMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public R delete(@RequestBody Long[] roleIds){
        sysRoleService.deleteBatch(roleIds);

        return R.ok();
    }*/



    /**
     * 订单调度页面需要的骑手信息
     * */
    @GetMapping("/findShopper")
    public  Result  findShopper(){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        Map<Object,Object> map2=new HashMap<Object, Object>();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //代理商登录
            map1.put("agentId",agentAccounts.getAgentid());
            map2.put("agentId",agentAccounts.getAgentid());
        }else if(admin !=null && admin.getType()==1){
            //后台登录---获取代理商list中第一个
           /* List<WaiMaiRunfastAgentbusiness> agentbusinessList=waiMaiRunfastAgentbusinessService.selectByExample(null);
            if(agentbusinessList.size()>0){
                exampleCriteria.andAgentidEqualTo(agentbusinessList.get(0).getId());
                example1Criteria.andAgentidEqualTo(agentbusinessList.get(0).getId());
            }*/
           return  Result.fail(null,"只有代理商才有此功能！");
        }
        map1.put("working",1);
        Integer  workingNum=waiMaiRunfastShopperService.countByMap(map1);
        map2.put("working",0);
        Integer  noWorkingNum=waiMaiRunfastShopperService.countByMap(map2);
        List<WaiMaiRunfastShopper>  workingList=waiMaiRunfastShopperService.selectByMap(map1);
        List<WaiMaiRunfastShopper>  noWorkingList=waiMaiRunfastShopperService.selectByMap(map2);
        setList(workingList);
        setList(noWorkingList);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("workingNum",workingNum);
        map.put("noWorkingNum",noWorkingNum);
        //map.put("list",list);
        map.put("workingList",workingList);
        map.put("noWorkingList",noWorkingList);
        return Result.ok(null,map);
    }
    public void setList( List<WaiMaiRunfastShopper> list){
        if(list.size()>0){
            for(WaiMaiRunfastShopper  shopper:list){
                Map<Object,Object> map=new HashMap<Object, Object>();
                map.put("shopperId",shopper.getId());
                map.put("startTime",TimeUtil.getStartTime());
                map.put("endTime",TimeUtil.getEndTime());
                map.put("jieDanOrPeiSongOrTotal",1);
                Integer jieDanNum=runfastGoodsSellRecordService.countJieDanOrPeiSongOrTotalByMap(map);
                shopper.setJieDanNum(jieDanNum);
                Map<Object,Object> map1=new HashMap<Object, Object>();
                map1.put("shopperId",shopper.getId());
                map1.put("startTime",TimeUtil.getStartTime());
                map1.put("endTime",TimeUtil.getEndTime());
                map1.put("jieDanOrPeiSongOrTotal",2);
                Integer peiSongNum=runfastGoodsSellRecordService.countJieDanOrPeiSongOrTotalByMap(map1);
                shopper.setPeiSongNum(peiSongNum);
                List<RunfastGoodsSellRecord> records=new ArrayList<RunfastGoodsSellRecord>();
                Map<Object,Object> map2=new HashMap<Object, Object>();
                map2.put("shopperId",shopper.getId());
                map2.put("startTime",TimeUtil.getStartTime());
                map2.put("endTime",TimeUtil.getEndTime());
                records=runfastGoodsSellRecordService.selectJieDanAndPeiSongByMap(map2);
                shopper.setRecords(records);
                Map<Object,Object> map3=new HashMap<Object, Object>();
                map3.put("shopperId",shopper.getId());
                map3.put("startTime",TimeUtil.getStartTime());
                map3.put("endTime",TimeUtil.getEndTime());
                map3.put("jieDanOrPeiSongOrTotal",3);
                Integer totalNum=runfastGoodsSellRecordService.countJieDanOrPeiSongOrTotalByMap(map3);
                shopper.setTotalNum(totalNum);
            }
        }
    }

    /**
     * 跑腿----订单调度页面需要的骑手信息
     * */
    @GetMapping("/paotuiFindShopper")
    public  Result  paotuiFindShopper(){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        Map<Object,Object> map2=new HashMap<Object, Object>();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //代理商登录
            map1.put("agentId",agentAccounts.getAgentid());
            map2.put("agentId",agentAccounts.getAgentid());
        }else if(admin !=null && admin.getType()==1){
            //后台登录---获取代理商list中第一个
            return  Result.fail(null,"只有代理商才有此功能！");
        }
        map1.put("working",1);
        Integer  workingNum=waiMaiRunfastShopperService.countByMap(map1);
        map2.put("working",0);
        Integer  noWorkingNum=waiMaiRunfastShopperService.countByMap(map2);
        List<WaiMaiRunfastShopper>  workingList=waiMaiRunfastShopperService.selectByMap(map1);
        List<WaiMaiRunfastShopper>  noWorkingList=waiMaiRunfastShopperService.selectByMap(map2);
        setPaotuiList(workingList);
        setPaotuiList(noWorkingList);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("workingNum",workingNum);
        map.put("noWorkingNum",noWorkingNum);
        map.put("workingList",workingList);
        map.put("noWorkingList",noWorkingList);
        return Result.ok(null,map);
    }
    public void setPaotuiList( List<WaiMaiRunfastShopper> list){
        if(list.size()>0){
            for(WaiMaiRunfastShopper  shopper:list){
                Map<Object,Object> map=new HashMap<Object, Object>();
                map.put("shopperId",shopper.getId());
                map.put("startTime",TimeUtil.getStartTime());
                map.put("endTime",TimeUtil.getEndTime());
                map.put("jieDanOrPeiSongOrTotal",1);
                Integer jieDanNum=orderService.countJieDanOrPeiSongOrTotalByMap(map);
                shopper.setJieDanNum(jieDanNum);
                Map<Object,Object> map1=new HashMap<Object, Object>();
                map1.put("shopperId",shopper.getId());
                map1.put("startTime",TimeUtil.getStartTime());
                map1.put("endTime",TimeUtil.getEndTime());
                map1.put("jieDanOrPeiSongOrTotal",2);
                Integer peiSongNum=orderService.countJieDanOrPeiSongOrTotalByMap(map1);
                shopper.setPeiSongNum(peiSongNum);
                List<Order> records=new ArrayList<Order>();
                Map<Object,Object> map2=new HashMap<Object, Object>();
                map2.put("shopperId",shopper.getId());
                map2.put("startTime",TimeUtil.getStartTime());
                map2.put("endTime",TimeUtil.getEndTime());
                records=orderService.selectJieDanAndPeiSongByMap(map2);
                if(records.size()>0){
                    for(Order  order:records){
                        orderService.findFromAndTo(order);
                    }
                }
                shopper.setPaotuiRecords(records);
                Map<Object,Object> map3=new HashMap<Object, Object>();
                map3.put("shopperId",shopper.getId());
                map3.put("startTime",TimeUtil.getStartTime());
                map3.put("endTime",TimeUtil.getEndTime());
                map3.put("jieDanOrPeiSongOrTotal",3);
                Integer totalNum=orderService.countJieDanOrPeiSongOrTotalByMap(map3);
                shopper.setTotalNum(totalNum);
            }
        }
    }



    /**所有上班骑手的列表*/
    @GetMapping("/findWorkShoppers")
    public Result  findWorkShoppers(){
        /**代理商id一定要有*/
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null && admin.getType()==2  && agentAccounts!=null){
            //AgentBusiness代理商
            WaiMaiRunfastAgentbusiness runfastAgentbusiness=runfastAgentbusinessService.selectByPrimaryKey(agentAccounts.getAgentid());
            if(runfastAgentbusiness!=null){
                WaiMaiRunfastShopperExample example=new WaiMaiRunfastShopperExample();
                example.createCriteria().andWorkingEqualTo(1).andStatusNotEqualTo(-1).andAgentidEqualTo(runfastAgentbusiness.getId());//1上班，其他休息
                //1/null：正常状态，-1：冻结状态(对应新需求的停用---冻结状态，启用--正常状态)
                //example.createCriteria().andStatusNotEqualTo(-1);
                List<WaiMaiRunfastShopper> list=waiMaiRunfastShopperService.selectByExample(example);
                Integer total=waiMaiRunfastShopperService.countByExample(example);
                Map<Object,Object> map=new HashMap<Object, Object>();
                map.put("list",list);
                map.put("total",total);
                return Result.ok(null,map);
            }
        }
        return Result.fail(null,"获取骑手列表失败！");
    }




}
